from ..extensions import db
from datetime import datetime

class Repair(db.Model):
    __tablename__ = 'repairs'
    
    id = db.Column(db.BigInteger().with_variant(db.Integer, "sqlite"), primary_key=True, autoincrement=True, comment='报修ID')
    house_id = db.Column(db.BigInteger().with_variant(db.Integer, "sqlite"), nullable=False, comment='报修户号，逻辑外键 houses.id')
    user_id = db.Column(db.BigInteger().with_variant(db.Integer, "sqlite"), nullable=False, comment='报修用户，逻辑外键 users.id')
    repair_type = db.Column(db.String(100), nullable=False, comment='报修类型')
    appointment_time = db.Column(db.DateTime, comment='预约时间')
    is_solved = db.Column(db.Boolean, nullable=False, default=False, comment='是否解决，0=未解决，1=已解决')
    created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow, comment='报修创建时间')
    
    def to_dict(self):
        return {
            'id': self.id,
            'house_id': self.house_id,
            'user_id': self.user_id,
            'repair_type': self.repair_type,
            'appointment_time': str(self.appointment_time) if self.appointment_time else None,
            'is_solved': bool(self.is_solved),
            'created_at': str(self.created_at)
        }